package com.zr.daoimpl;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.zr.dao.CarDaoZYH;
import com.zr.model.Deliverypoint;
import com.zr.vo.Car_DpVO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Repository("CarDaoZYH")
public class CarDaoImplZYH<T> extends BaseDaOImpl<T> implements CarDaoZYH {
	
	@Override
	public long findCarCount() {
		String hql = "select count(c.carId) from Car as c";
		Query q = this.getCurrentSession().createQuery(hql);
		List<Object> ob = q.list();
		//String s = ob.get(0).toString();
		//int total = Integer.parseInt(ob.get(0).toString());
		long total = (long) ob.get(0);
		return  total;
	}
	@Override
	public JSONArray getCarInfo(int page,int rows) {
		JSONArray ja = new JSONArray();
		String sql = "select c.carId,c.carName,c.carType,c.carState,c.carMaxWeight,c.carNumber,d.dpName"
		+" from car as c,deliverypoint as d"
		+" where c.dpId = d.dpId";
		Query q = this.getCurrentSession().createSQLQuery(sql);
		q.setFirstResult((page-1)*rows);
		q.setMaxResults(rows);
		List<Car_DpVO> cds = q.setResultTransformer(Transformers.aliasToBean(Car_DpVO.class)).list();
		for (Car_DpVO cd:cds) {
			JSONObject j = new JSONObject();
			j.put("carId", cd.getCarId());
			j.put("carName", cd.getCarName());
			j.put("carMaxWeight", cd.getCarMaxWeight());
			j.put("carType", cd.getCarType());
			j.put("carState", cd.getCarState());
			j.put("carNumber", cd.getCarNumber());
			j.put("dpName", cd.getDpName());
			ja.add(j);
		};
		return ja;
	}

	@Override
	public void insertCar(String carName,String carType,String carNumber ,int carMaxWeight,String carState,int dpId) {
		String sql = " insert car(carName,carType,carNumber,carMaxWeight,carState,dpId)"
				+"values(:carName,:carType,:carNumber,:carMaxWeight,:carState,:dpId)";
		Query q = this.getCurrentSession().createSQLQuery(sql);
		q.setParameter("carName", carName);
		q.setParameter("carType",carType );
		q.setParameter("carNumber",carNumber );
		q.setParameter("carMaxWeight",carMaxWeight );
		q.setParameter("carState", carState);
		q.setParameter("dpId", dpId);
		q.executeUpdate();
	}

	

	@Override
	public JSONArray getDpidAndDpname() {
		JSONArray ja = new JSONArray();
		String hql = " from Deliverypoint";
		Query q = this.getCurrentSession().createQuery(hql);
		List<Deliverypoint> dps = q.list();
		for (Deliverypoint dp : dps) {
			JSONObject j = new JSONObject();
			j.put("id", dp.getDpId().intValue());
			j.put("text", dp.getDpName());
			ja.add(j);
		}
		return ja;
	}

	@Override
	public boolean updateDpByCarId(int carId,String dpName) {
		boolean bl = false;
		String sql = " update car c set c.dpId=:dpId where c.carId=:carId";
		Query q = this.getCurrentSession().createSQLQuery(sql);
		q.setParameter("dpId", this.findDpIdByDpName(dpName));
		q.setParameter("carId", carId );
		int i = q.executeUpdate();
		if(i>0){
			bl = true;
		}
		return bl;
	}

	@Override
	public int findDpIdByDpName(String DpName) {
		int dpId = 0;
		String hql = "select dpId from Deliverypoint as dp where dp.dpName=:dpName";
		Query q = this.getCurrentSession().createQuery(hql);
		q.setParameter("dpName", DpName);
		List<Integer> dps = q.list();
		for (Integer dp: dps) {
			dpId = dp.intValue();
		}
		return dpId;
	}

	@Override
	public int findCarNumber(String carNum) {
		int carId = 0;
		String sql="select carId from car as c where c.carNumber=:carNum";
		Query q = this.getCurrentSession().createSQLQuery(sql);
		q.setParameter("carNum", carNum);
		List<Integer> cc = q.list();
		for (Integer c : cc) {
			carId = c.intValue();
		}
		return carId;
	}
	

}
